package com.lgx.mobile.dao.impl;

import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.lgx.common.basic.dao.GeneralDao;
import com.lgx.common.exception.DataException;
import com.lgx.mobile.dao.IGiftDao;
import com.lgx.mobile.entity.Gift;

/**
 * 
 * @author xuyichen
 *
 */
@Component("giftDao")
public class GiftDao extends GeneralDao implements IGiftDao {

	protected Logger log = Logger.getLogger(this.getClass());
	
	public Gift createGift(Gift gift) throws DataException {
		this.getHibernateTemplate().save(gift);
		log.info("[创建礼品]:"+gift.getId());
		return gift;
	}

	public Gift updateGift(Gift gift) throws DataException {
		this.getHibernateTemplate().update(gift);
		log.info("[更新礼品]:"+gift.getId());
		return gift;
	}

	public void deleteGift(Gift gift) throws DataException {
		log.info("[删除礼品]:"+gift.getId());
		this.getHibernateTemplate().delete(gift);
	}

	public void deleteGiftById(long id) throws DataException {
		log.info("[删除礼品]:"+id);
		this.deleteGift(this.findGiftById(id));
	}

	public Gift findGiftById(long id) {
		return this.getHibernateTemplate().get(Gift.class, id);
	}
	
	@SuppressWarnings("unchecked")
	public List<Gift> findGiftList(){
		String sql = "from Gift g order by g.id asc";
		return this.getHibernateTemplate().find(sql);
	}
}
